package com.dongdongsongcan.app.db;

import java.util.List;

import com.dongdongsongcan.app.db.bean.ShoppingCarDBBean;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.su.dolphin.model.db.DpBaseDao;
import com.su.dolphin.utils.LogUtil;

public class ShoppingCarDao extends DpBaseDao {

    public static void clearShopingCar(int shopid) {
        try {
            getDb().delete(ShoppingCarDBBean.class,
                    WhereBuilder.b("shopid", "=", shopid));
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public static void delete(int productId) {
        try {
            ShoppingCarDBBean shoppingCarDBBean = getDb().findFirst(
                    Selector.from(ShoppingCarDBBean.class).expr("productid",
                            "=", productId));
            if (shoppingCarDBBean != null) {
                if (shoppingCarDBBean.getCount() == 1) {// 如果还有一件商品, 直接删除这个商品
                    getDb().delete(ShoppingCarDBBean.class,
                            WhereBuilder.b("productid", "=", productId));
                } else {// 有多个商品, 删除一个
                    shoppingCarDBBean
                            .setCount(shoppingCarDBBean.getCount() - 1);
                    getDb().update(shoppingCarDBBean,
                            WhereBuilder.b("productid", "=", productId), "count");
                    // 打印一下更改之后的数据
                    shoppingCarDBBean = getDb().findFirst(
                            Selector.from(ShoppingCarDBBean.class).expr(
                                    "productid", "=", productId));
                    LogUtil.d(shoppingCarDBBean.toString());

                }
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public static void save(ShoppingCarDBBean shoppingCarDBBean) {
        try {
            ShoppingCarDBBean shoppingCarDBBeanExist = getDb().findFirst(
                    Selector.from(ShoppingCarDBBean.class).expr("productid",
                            "=", shoppingCarDBBean.getProductid()));
            if (shoppingCarDBBeanExist == null) {
                getDb().save(shoppingCarDBBean);
            } else {
                shoppingCarDBBeanExist.setCount(shoppingCarDBBeanExist
                        .getCount() + 1);
//				getDb().update(
//						shoppingCarDBBeanExist,
//						WhereBuilder.b("proudctid", "=",
//								shoppingCarDBBeanExist.getProductid()), "count");

                getDb().execNonQuery("update shoppingcar set count = " + shoppingCarDBBeanExist.getCount() + " where productid = " + shoppingCarDBBean.getProductid());
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    public static ShoppingCarDBBean findFirst(int shopId, int productId) {
        try {
            return getDb().findFirst(
                    Selector.from(ShoppingCarDBBean.class)
                            .expr("shopid", "=", shopId)
                            .and("productid", "=", productId));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<ShoppingCarDBBean> getDataByShopId(int shopId) {
        try {
            List<ShoppingCarDBBean> shopCarInfos = getDb().findAll(
                    Selector.from(ShoppingCarDBBean.class).expr("shopid", "=",
                            shopId));
            // if (shopCarInfos != null) {
            // for (int i = 0; i < shopCarInfos.size(); i++) {
            // LogUtil.d(shopCarInfos.get(i).toString());
            // }
            // }

            return shopCarInfos;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

}
